package com.tjhzzx.danfossNiagaraCore.alarm;

import com.tridium.alarm.AlarmsToSourcesCursor;

import javax.baja.alarm.BAlarmRecipient;
import javax.baja.alarm.BAlarmRecord;
import javax.baja.alarm.BAlarmService;
import javax.baja.collection.BITable;
import javax.baja.naming.BOrd;
import javax.baja.naming.SlotPath;
import javax.baja.security.BPermissions;
import javax.baja.security.PermissionException;
import javax.baja.sys.*;
import javax.baja.user.BUser;
import javax.baja.user.BUserService;

//import javax.baja.nre.util.Array;


public class BTlsAlarmRecipient extends BAlarmRecipient {

//    private Array channels;

    public static final Action queryAlarm = newAction(0, null);

    public void queryAlarm() {
        invoke(queryAlarm, null, null);
    }

    public void doQueryAlarm() {
        String str = getInitialAlarmQuery(null);
        System.out.println(str);
    }

    public BIcon getIcon() {
        return BIcon.make(BIcon.std("console.png"), BIcon.std("badges/alarm.png"));
    }

    public Cursor getOpenAlarmSources(Context paramContext) {
        return new AlarmsToSourcesCursor(getOpenAlarms(paramContext));
    }

    public Cursor getOpenAlarms(Context paramContext) {
        String str = getInitialAlarmQuery(paramContext);
        return ((BITable) BOrd.make(str).resolve(this).get()).cursor();
    }

    public String getInitialAlarmQuery(Context paramContext) {
        BUser localBUser = null;
        if (paramContext != null) {
            localBUser = paramContext.getUser();
        }
        if (localBUser == null) {
            BUserService localBUserService = (BUserService) Sys.getService(BUserService.TYPE);
            localBUser = localBUserService.getAdmin();
            if (!localBUser.getEnabled()) {
                BUser[] arrayOfBUser = localBUserService.getChildren(BUser.class);
                for (int i = 0; i < arrayOfBUser.length; i++) {
                    if ((arrayOfBUser[i].getEnabled()) && (arrayOfBUser[i].getPermissions().isSuperUser())) {
                        localBUser = arrayOfBUser[i];
                        break;
                    }
                }
            }
        }
        return getAlarmQuery(localBUser);
    }

    public String getAlarmQuery(BUser paramBUser) {
        BAlarmService localBAlarmService = (BAlarmService) Sys.getService(BAlarmService.TYPE);
        String[] arrayOfString = getSubscribedAlarmClasses();
        StringBuffer localStringBuffer1 = new StringBuffer("local:|foxs:|alarm:|bql:select * from openAlarms");
        int i = 0;
        StringBuffer localStringBuffer2 = new StringBuffer();
        for (int j = 0; j < arrayOfString.length; j++) {
            try {
                String str = arrayOfString[j];
                paramBUser.check(localBAlarmService.lookupAlarmClass(str), BPermissions.operatorRead);
                if (i > 0) {
                    localStringBuffer2.append(" or ");
                } else {
                    localStringBuffer2.append("(");
                }
                i++;
                localStringBuffer2.append("alarmClass = '" + SlotPath.escape(str) + '\'');
            } catch (PermissionException localPermissionException1) {
            }
        }
        if (i > 0) {
            localStringBuffer2.append(")");
        }
        if (localStringBuffer2.length() > 0) {
            localStringBuffer1.append(" where (");
            localStringBuffer1.append(localStringBuffer2.toString() + ')');
        } else {
            localStringBuffer1.append(" where alarmClass = 'null'");
        }
        localStringBuffer1.append(" and ackState='unacked' sourceState='offnormal'");
        return localStringBuffer1.toString();
    }

    @Override
    public void handleAlarm(BAlarmRecord bAlarmRecord) {
    }

    public static final Type TYPE = Sys.loadType(BTlsAlarmRecipient.class);

    public Type getType() {
        return TYPE;
    }
}
